/***********************

DESCRIPTION: Reads in original datasets and treatment characteristics

Programs used: "_C. Disease Intensity.do"

Datasets Used:
	(1)

Datasets Created:
	(1) 

***********************/


set more off
capture log close
global path "N:\MedicareClaims-P045601-BE\Work\"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global mfixformat "%02.0f"
log using method2, replace
cd N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out
do "_C. Disease Intensity.do"

	foreach year of numlist 2005/2012 {

		use "$path\`year'\medpar_all_file.dta", clear 
		gen secondary_hyp=0 
		gen primary_hyp=0 
		gen unspecified=0 
		gen benign=0 
		gen malignant=0 
		gen type_II=0 
		gen complication=0
		foreach k of numlist 1/25 {
			rename DGNS_`k'_CD ICD_DGNS_CD`k'
		}
		forvalues n = 1/12 {
			disease_intensity ICD_DGNS_CD`n' 
		}

	keep if secondary_hyp==1 | primary_hyp==1 | unspecified==1 | benign==1 | malignant==1 | type_II==1 | complication==1
	save "$path\`year'\mpr_intensity", replace

	}

	foreach year of numlist 2005/2012 {
		use "$path\`year'\outpatient_base_claims_j.dta", clear
			gen secondary_hyp=0 
		gen primary_hyp=0 
		gen unspecified=0 
		gen benign=0 
		gen malignant=0 
		gen type_II=0 
		gen complication=0
		forvalues n = 1/12{
			disease_intensity ICD_DGNS_CD`n'

		}
		keep if secondary_hyp==1 | primary_hyp==1 | unspecified==1 | benign==1 | malignant==1 | type_II==1 | complication==1
	save "$path\`year'\outp_intensity", replace
	}


	foreach year of numlist 2005/2012 {
		use "$path\`year'\bcarrier_claims_j.dta",clear
			gen secondary_hyp=0 
		gen primary_hyp=0 
		gen unspecified=0 
		gen benign=0 
		gen malignant=0 
		gen type_II=0 
		gen complication=0
		forvalues n =1/12{
			disease_intensity ICD_DGNS_CD`n'
		}
		keep if secondary_hyp==1 | primary_hyp==1 | unspecified==1 | benign==1 | malignant==1 | type_II==1 | complication==1
	save "$path\`year'\bcar_intensity", replace
	}



	local diabetes_out 355 379 362 369 354 367 368 331 781 410 /// 
		429 428 436 427 459 785 413 414 444 453 V81 424 440 ///
		458 447 599 593 586 590 596 592 782 707 709 686 883 ///
		879 882 892 891 893 884
	local hyp_out 410 413 414 427 428 429 436 444 459 785
	local chronic 714 715 716 721 729 724 722 732 733 696 707 703 700 216 172 ///
		173 366 365 361 571 572 531 532 533 550 551 552 553 530 535 787 555 556 558 ///
		562 564 534 537 560 569 151 152 153 154 240 241 243 244 245 246 250 280 281 ///
		282 283 284 285 345 346 784 592 590 581 582 583 593 596 600 602 340 614 615 ///
		616 620 621 622 623 624 626 617 618 619 625 627 174 179 180 181 182 183 184 ///
		185 217 218 219 220 221 390 392 393 394 395 396 397 398 399 410 411 412 413 ///
		414 745 746 415 416 417 423 424 426 428 401 402 403 404 405 430 431 432 433 ///
		434 435 437 440 442 451 454 455 490 491 493 477 471 473 470 474 476 492 511 ///
		500 501 502 503 504 505 011 019 515 518 160 161 163 X00 X01 X02 X03 X05 X06 ///
		X07 X08 X09 X10 X11 X12 X19 X20 X21 X23 X24 X22 X25 X26 X28 X27 X29 X30 X31 ///
		X32 X34 X35 X40 X41 X46 X42 X43 X44 X45 X47 X48 X49 X50 X51 X56 X52 X53 X54 ///
		X55 X57 X58 X59 X60 X61 X62 X63 X64 X70 X71 X80 X74 X84 X73 X77 X78 X75 X76 ///
		X85 X86 X79 X89 X91   
	local hyp 401 405 642
	local diabetes 250
	
	foreach year of numlist 2005(1)2012{
		
		use "$path\`year'\outpatient_base_claims_j.dta", clear
		gen keep=0
		foreach i in diabetes_out hyp_out chronic hyp diabetes {
			foreach j of local `i' {
				gen `i'_`j' = 0
				*replace `i'_`j'=1 if substr(PRNCPAL_DGNS_CD,1,3)=="`j'"
				foreach k of numlist 1/12 {
					replace `i'_`j'=1 if substr(ICD_DGNS_CD`k',1,3)=="`j'"
					replace keep=1 if `i'_`j'==1
				}	
			}
		}
		keep if keep==1
		save "$path\`year'\op_treats", replace 
		clear
		
		use "$path\`year'\medpar_all_file.dta", clear 
		gen keep=0
		foreach i in diabetes_out hyp_out chronic hyp diabetes {
			foreach j of local `i' {
				gen `i'_`j' = 0
				foreach k of numlist 1/12 {
					replace `i'_`j'=1 if substr(ICD_DGNS_CD`k',1,3)=="`j'"
					replace keep=1 if `i'_`j'==1
				}
			}
		}
		keep if keep==1
		save "$path\`year'\mpr_treats", replace

		use "$path\`year'\bcarrier_claims_j.dta", clear
		gen keep=0
		foreach i in diabetes_out hyp_out chronic hyp diabetes {
			foreach j of local `i' {
				gen `i'_`j' = 0
				*replace `i'_`j'=1 if substr(PRNCPAL_DGNS_CD,1,3)=="`j'"
				foreach k of numlist 1/12 {
					replace `i'_`j'=1 if substr(ICD_DGNS_CD`k',1,3)=="`j'"
					replace keep=1 if `i'_`j'==1
				}
			}
		}
		keep if keep==1
		save "$path\`year'\car_treats", replace
	}



